
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aljfce.business.entity;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author jacopo
 */
@Entity
@Table(name = "Calendar")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Calendar.findAll", query = "SELECT c FROM Calendar c"),
    @NamedQuery(name = "Calendar.findByUser", query = "SELECT c FROM Calendar c WHERE c.calendarPK.user = :user"),
    @NamedQuery(name = "Calendar.findByCalendarName", query = "SELECT c FROM Calendar c WHERE c.calendarPK.calendarName = :calendarName"),
    @NamedQuery(name = "Calendar.findByVisibility", query = "SELECT c FROM Calendar c WHERE c.visibility = :visibility")})
public class Calendar implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected CalendarPK calendarPK;
    @Basic(optional = false)
    @NotNull
    @Column(name = "Visibility")
    private boolean visibility;
    @JoinTable(name = "Calendar_Event", joinColumns = {
        @JoinColumn(name = "Calendar_User", referencedColumnName = "User"),
        @JoinColumn(name = "Calendar_CalendarName", referencedColumnName = "CalendarName")}, inverseJoinColumns = {
        @JoinColumn(name = "Event_Id", referencedColumnName = "Id")})
    @ManyToMany
    private Collection<Event> eventCollection;
    @JoinColumn(name = "User", referencedColumnName = "Email", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private User user;
    
    //private Collection<EventWrapper> eventWCollection;

    public Calendar() {
    }

    public Calendar(CalendarPK calendarPK) {
        this.calendarPK = calendarPK;
        this.visibility = false;
    }

    public Calendar(CalendarPK calendarPK, boolean visibility) {
        this.calendarPK = calendarPK;
        this.visibility = visibility;
    }

    public Calendar(String user, String calendarName) {
        this.calendarPK = new CalendarPK(user, calendarName);
    }

    public CalendarPK getCalendarPK() {
        return calendarPK;
    }

    public void setCalendarPK(CalendarPK calendarPK) {
        this.calendarPK = calendarPK;
    }

    public boolean getVisibility() {
        return visibility;
    }

    public void setVisibility(boolean visibility) {
        this.visibility = visibility;
    }

    @XmlTransient
    public Collection<Event> getEventCollection() {
        return eventCollection;
    }
    
    /*public Collection<EventWrapper> getWEventCollection() {
        return eventWCollection;
    }*/

    public void setEventCollection(Collection<Event> eventCollection) {
        this.eventCollection = eventCollection;
            
        /*this.eventWCollection = null;
        List<Event> evntC = (List<Event>)this.eventCollection;
        for(int i = 0; i < evntC.size(); i++){
            this.eventWCollection.add(new EventWrapper(evntC.get(i)));
        }*/
    }
    
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (calendarPK != null ? calendarPK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Calendar)) {
            return false;
        }
        Calendar other = (Calendar) object;
        if ((this.calendarPK == null && other.calendarPK != null) || (this.calendarPK != null && !this.calendarPK.equals(other.calendarPK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.aljfce.business.entity.Calendar[ calendarPK=" + calendarPK + " ]";
    }
    
    public void addEvent(Event event){
        boolean add = this.eventCollection.add(event);
        
        /*for(int i = 0; i < eventCollection.size(); i++){
            this.eventWCollection.add(new EventWrapper(event));
        }*/
    }

    public void removeEvent(int eventId) {
        List<Event> eventList = (List<Event>) eventCollection;
        //List<EventWrapper> eventWList = (List<EventWrapper>) eventWCollection;
        for(int i = 0; i < eventList.size(); i++){
            if(eventList.get(i).getId() == eventId){
                eventCollection.remove(eventList.get(i));
                //eventWCollection.remove(eventWList.get(i));
                break;
            }
        }
    }
    
}

